import java.util.regex.Pattern;


public class FileManager {
	
	private String line;
	private String torre;
	private String torreLink;
	private TypeLink typeLink;
	
	private TorreManager manager=new TorreManager();
	
	public String processLine(String line){
		String result="";
		
		torre = "";
		torreLink = "";
		typeLink = TypeLink.INVALID;
		
		//Validamos la linea
		if (isValidLine(line)){
			this.line = line.replaceAll("\\s", "");
			
			//En caso de que sea valido, tratamos de obtener el nombre de la torre y su accion
			searchNameTorre();
			
			switch(typeLink){
				case ADDLEFT:
				case ADDRIGHT:
					manager.addTorre(torre, torreLink);
					break;
				case QUESTIONLEFT:
				case QUESTIONRIGHT:
					result = manager.getRoute(torre, torreLink);
					break;
				case REMOVE:
					manager.removeLink(torre, torreLink);
					break;
			}
		}
		
		return result;
	}
	
	public void searchNameTorre(){
		String[] split;		
		//Establecemos el tipo de enlace
		searchType();
		
		//Es un link
		switch(typeLink){
			case ADDLEFT:
				split = line.split(OperatorSymbol.ADDLEFT_OPERATOR);
				torre = split[0];
				//Se remueve la basura
				torreLink = split[1].replace(split[1].substring(split[1].indexOf(OperatorSymbol.LINK_FINALOPERATOR)), "");
				break;
			case ADDRIGHT:
				split = line.split(OperatorSymbol.ADDRIGHT_OPERATOR);
				torreLink = split[0];
				torre = split[1].replace(split[1].substring(split[1].indexOf(OperatorSymbol.LINK_FINALOPERATOR)), "");
				break;
			case QUESTIONLEFT:
				split = line.split(OperatorSymbol.QUESTIONLEFT_OPERATOR);
				torre = split[0];
				torreLink = split[1].replace(split[1].substring(split[1].indexOf(OperatorSymbol.QUESTION_FINALOPERATOR)), "");
				break;
			case QUESTIONRIGHT:
				split = line.split(OperatorSymbol.QUESTIONRIGHT_OPERATOR);
				torreLink = split[0];
				torre = split[1].replace(split[1].substring(split[1].indexOf(OperatorSymbol.QUESTION_FINALOPERATOR)), "");
				break;
			case REMOVE:
				split = line.split(OperatorSymbol.REMOVE_OPERATOR);
				torre = split[0];
				torreLink = split[1].replace(split[1].substring(split[1].indexOf(OperatorSymbol.LINK_FINALOPERATOR)), "");
				break;
		}
		
		torre = torre.trim();
		torreLink = torreLink.trim();
	}
	
	private void searchType(){
		final String questionLeft = "[a-zA-Z]{1}[\\s\\w]{0,99}(=>){1}[a-zA-Z]{1}[\\s\\w]{0,99}\\?.*";
		final String questionRight = "[a-zA-Z]{1}[\\s\\w]{0,99}(<=){1}[a-zA-Z]{1}[\\s\\w]{0,99}\\?.*";
		final String addLeft = "[a-zA-Z]{1}[\\s\\w]{0,99}(->){1}[a-zA-Z]{1}[\\s\\w]{0,99}\\..*";
		final String addRight = "[a-zA-Z]{1}[\\s\\w]{0,99}(<-){1}[a-zA-Z]{1}[\\s\\w]{0,99}\\..*";
		final String remove = "[a-zA-Z]{1}[\\s\\w]{0,99}-{1}[a-zA-Z]{1}[\\s\\w]{0,99}\\..*";
		
		if (Pattern.matches(questionLeft,line)){
			typeLink = TypeLink.QUESTIONLEFT;
		}else if (Pattern.matches(questionRight,line)){
			typeLink = TypeLink.QUESTIONRIGHT;
		}else if (Pattern.matches(addLeft,line)){
			typeLink = TypeLink.ADDLEFT;
		}else if (Pattern.matches(addRight,line)){
			typeLink = TypeLink.ADDRIGHT;
		}else if (Pattern.matches(remove,line)){
			typeLink = TypeLink.REMOVE;
		}
	}
	
	/**
	 * Evalua si es valida la linea 
	 * @return
	 */
	private static boolean isValidLine(String line){
		boolean result = false;
		
		if (line!=null && !line.isEmpty()){
			//Se valida el formato de la linea
			
			result = true;
		}
		
		return result;
	}
}
